﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinqToLdap.Samples.Core.Domain;
using LinqToLdap.Samples.Mvc.Models;
using StructureMap;

namespace LinqToLdap.Samples.Mvc.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult People()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetPeople(string q)
        {
            var context = ObjectFactory.GetInstance<IDirectoryContext>();

            var query = context.Query<Person>();

            if (!string.IsNullOrWhiteSpace(q))
            {
                var expression = PredicateBuilder.Create<Person>().Or(p => p.CommonName == q);
                var criteria = q.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                expression = criteria.Aggregate(expression,
                                                (current, criterion) =>
                                                    current.Or(
                                                        p =>
                                                        p.FirstName.StartsWith(criterion) ||
                                                        p.LastName.StartsWith(criterion)));

                query = query.Where(expression);
            }

            var results = query.Take(150) //maximum number of results allowed from the server.
                .ToList();

            var data = new JqGridModel<string>
                           {
                               total = 150,
                               records = 150,
                               rows = results.Select(p => new JqGridCellModel<string>
                                                              {
                                                                  id = p.DistinguishedName,
                                                                  cell = new[]
                                                                             {
                                                                                 p.CommonName,
                                                                                 p.Titles.FirstOrDefault(),
                                                                                 p.Email,
                                                                                 p.PhoneNumbers.FirstOrDefault()
                                                                             }
                                                              }
                                   ).ToArray()
                           };

            return Json(data);
        }
    }
}
